/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util.httpclient;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Map;
import java.util.Map.Entry;

import com.enfry.common.util.validate.Validates;

/**
 * 
 * HTTPS 文件上传类
 * 
 * @Title com.enfry.util.httpclient.HttpUpload
 * @author Penghu
 * @date 2017年11月8日
 */
public class HttpUpload extends HttpClient {
	// 上传文件和参数
	private String name;
	private String fileName;
	private byte[] fileContent;
	private String queryString;
	private boolean hasEncoded;

	// 上传内容格式
	private String mimeType;
	private String encoding;
	private String boundary;

	public HttpUpload(String url) {
		super(url, _POST_);
		this.mimeType = _APPLICATION_OCTET_STREAM_;// 默认字节流
		this.encoding = _UTF_8_;
		this.boundary = "" + System.currentTimeMillis();
	}

	/**
	 * 添加文件内容
	 * 
	 * @param name 文件上传时使用的字段名称
	 * @param fileName 文件名
	 * @param fileContent 文件内容
	 * @param encoding 文件编码
	 */
	public void setContent(String name, String fileName, byte[] fileContent, String encoding) {
		this.name = name;
		this.fileName = fileName;
		this.fileContent = fileContent;
		this.encoding = encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	public void setQueryString(String queryString, boolean hasEncoded) {
		this.queryString = queryString;
		this.hasEncoded = hasEncoded;
	}

	protected boolean doPreRequestProperty() {
		if (Validates.isEmpty(fileName) || fileContent == null) {
			responseStatus = _99_EXCEPTION_;
			responseText = "上传的文件名和内容不能为空";
			return false;
		}

		setDoOutput(true);
		addRequestProperty(_CONTENT_TYPE_, _MULTIPART_FORM_DATA_ + "; charset=" + encoding
				+ "; boundary=" + boundary);
		return true;
	}

	protected void doWriteRequestContent(HttpURLConnection conn) throws IOException {
		OutputStream out = conn.getOutputStream();
		byte[] entryBoundaryBytes = (_BR_ + "--" + boundary + _BR_).getBytes(encoding);

		// 输入文本请求参数
		Map<String, String> paramMap = null;
		if (hasEncoded)
			paramMap = UrlUtils.toMap(queryString, encoding);
		else
			paramMap = UrlUtils.toMapNoEncoded(queryString);

		for (Entry<String, String> entry : paramMap.entrySet()) {
			byte[] textBytes = getTextDisposition(entry.getKey(), encoding);
			out.write(entryBoundaryBytes);
			out.write(textBytes);
			out.write(entry.getValue().getBytes(encoding));
		}

		// 输入文件请求参数
		byte[] fileBytes = getFileDisposition(name, fileName, mimeType, encoding);
		out.write(entryBoundaryBytes);
		out.write(fileBytes);
		out.write(fileContent);

		// 输入结束标志
		byte[] endBoundaryBytes = (_BR_ + "--" + boundary + "--" + _BR_).getBytes(encoding);
		out.write(endBoundaryBytes);
	}
}
